package com.lemner.ke.fragment;

import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lemner.ke.R;
import com.lemner.ke.activity.DetailActivity;
import com.lemner.ke.activity.HomeActivity;
import com.lemner.ke.adapter.RecommendRecAdapter;
import com.lemner.ke.base.BaseFragment;
import com.lemner.ke.base.BaseListBean;
import com.lemner.ke.base.BaseListener;
import com.lemner.ke.base.URL;
import com.lemner.ke.bean.RecommedBean;
import com.lemner.ke.bean.RecommendBannerBean;
import com.lemner.ke.bean.RecommendBean;
import com.lemner.ke.model.knowledge.RecommendBannerModel;
import com.lemner.ke.model.knowledge.RecommendModel;
import com.lemner.ke.util.BannerImageLoader;
import com.lemner.ke.util.SeeMoreListener;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

/**
 * 推荐
 *
 * @author raintea
 * @date 2018/9/20
 */
public class RecommendFragment extends BaseFragment {

    RecyclerView rec;
    RecommendRecAdapter recAdapter;
    Banner banner;
    private List<String> bannerImgs;
    SeeMoreListener listener;
    public void setMoreListener(SeeMoreListener listener){
        this.listener=listener;
    }

    public static RecommendFragment getInstance() {
        return new RecommendFragment();
    }

    @Override
    public int getLayoutFile() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void initSetting() {

    }

    @Override
    public void initView() {
//        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(getContext()));
        rec = findViewById(R.id.rv_recommend);

//        List<RecommedBean> list = new ArrayList<>();
//        for (int i = 0; i < 20; i++) {
//            RecommedBean bean = new RecommedBean();
//            bean.setTitle("你好" + i);
//            list.add(bean);
//        }

        //创建适配器
        recAdapter = new RecommendRecAdapter(R.layout.item_new_knowledge);
        //创建头布局
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.header_recommend, null);
        banner = inflate.findViewById(R.id.b_header_recommend);
        initBanner();
        createHeadView(inflate);

        initRecAdapter();

    }

    private void initBanner() {

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setImageLoader(new BannerImageLoader());
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.CubeOut);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        // 获取轮播图数据
        new RecommendBannerModel().getBannerList(new BaseListener() {
            @Override
            public void onResponse(Object o) {
                bannerImgs = new ArrayList<>();
                if (o != null) {
                    BaseListBean<RecommendBannerBean> bean = (BaseListBean<RecommendBannerBean>) o;
                    if (bean != null) {
                        List<RecommendBannerBean> beanList = bean.getData();
                        if (beanList != null && beanList.size() > 0) {
                            for (RecommendBannerBean rbb : beanList) {
                                bannerImgs.add(URL.BASE_URL + rbb.getPath());
                            }
                            banner.setImages(bannerImgs);
                            //banner设置方法全部调用完毕时最后调用
                            banner.start();
                        }
                    }
                }
            }

            @Override
            public void onFail(String msg) {

            }
        });
        // 获取列表数据
        new RecommendModel().getRecommendList(new BaseListener() {
            @Override
            public void onResponse(Object o) {
                if (o != null) {
                    BaseListBean<RecommendBean> bean = (BaseListBean<RecommendBean>) o;

                    List<RecommendBean> datas = bean.getData();
                    recAdapter.addData(datas);

                }
            }

            @Override
            public void onFail(String msg) {

            }
        });

    }

    @Override
    public void onClick(View v) {

    }

    //添加头布局
    public void createHeadView(View v) {
        LinearLayout viewById = v.findViewById(R.id.ll_more);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.seeMore();
            }
        });
        recAdapter.addHeaderView(v);
    }

    public void initRecAdapter() {
        //创建布局管理
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rec.setLayoutManager(linearLayoutManager);
        //条目跳转
        recAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(getContext(), "点击了" + position, Toast.LENGTH_SHORT).show();
                RecommendBean bean= (RecommendBean) adapter.getItem(position);
                Intent intent=new Intent(getContext(),DetailActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("recommend",bean);
                intent.putExtras(bundle);

                startActivity(intent);

            }
        });
        recAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getContext(), "长点击了" + position, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        rec.setAdapter(recAdapter);
    }

}
